package com.echo.register.hosp.api;

import com.echo.register.common.exception.RegisterException;
import com.echo.register.common.helper.HttpRequestHelper;
import com.echo.register.common.result.Result;
import com.echo.register.common.result.ResultCodeEnum;
import com.echo.register.hosp.service.DepartmentService;
import com.echo.register.hosp.service.HospitalService;
import com.echo.register.hosp.service.HospitalSetService;
import com.echo.register.hosp.service.ScheduleService;
import com.echo.register.model.hosp.Department;
import com.echo.register.model.hosp.Hospital;
import com.echo.register.model.hosp.Schedule;
import com.echo.register.vo.hosp.DepartmentQueryVo;
import com.echo.register.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: ApiController
 * @Package: com.echo.register.hosp.controller
 * @Description: 医院管理API接口
 * @Author 谢同学
 * @Create 2023/12/19 11:37
 * @Version 1.0
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "上传医院")
    @PostMapping("/saveHospital")
    public Result<?> saveHospital(HttpServletRequest request) {
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        // 调用Api
        hospitalService.save(objectMap);
        return Result.ok();
    }

    @ApiOperation(value = "查询医院")
    @PostMapping("/hospital/show")
    public Result<Hospital> getHospital(HttpServletRequest request) {
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        // 获取医院编号
        String hoscode = (String) objectMap.get("hoscode");
        // 调用api
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    // 科室上传
    @ApiOperation(value = "科室上传")
    @PostMapping("/saveDepartment")
    public Result<?> saveDepartment(HttpServletRequest request) {
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        // 调用api
        departmentService.saveDepartment(objectMap);
        return Result.ok();
    }

    @ApiOperation(value = "查询科室")
    @PostMapping("/department/list")
    private Result<Page<Department>> getDepartment(HttpServletRequest request) {
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        // 获取参数
        String hoscode = (String) objectMap.get("hoscode");
        int page = StringUtils.isEmpty(objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        int limit = StringUtils.isEmpty(objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));


        if(StringUtils.isEmpty(hoscode)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> departmentPage = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("/department/remove")
    public Result<?> deleteDepartment(HttpServletRequest request){
        Map<String, Object> objectMap = this.handleParam(request);

        String hoscode = (String) objectMap.get("hoscode");
        String depcode = (String) objectMap.get("depcode");

        departmentService.deleteDepartment(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation("上传排班信息")
    @PostMapping("saveSchedule")
    public Result<?> saveSchedule(HttpServletRequest request){
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        scheduleService.saveSchedule(objectMap);
        return Result.ok();
    }

    @ApiOperation("查询排版信息")
    @PostMapping("/schedule/list")
    public Result<Page<Schedule>> getSchedule(HttpServletRequest request){
        // 处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        // 获取参数
        String hoscode = (String) objectMap.get("hoscode");
        int page = StringUtils.isEmpty(objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        int limit = StringUtils.isEmpty(objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));


        if(StringUtils.isEmpty(hoscode)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);


        Page<Schedule> schedule = scheduleService.getSchedule(page, limit, scheduleQueryVo);
        return Result.ok(schedule);
    }
    @ApiOperation("删除排班")
    @PostMapping("/schedule/remove")
    private Result<?> deleteSchedule(HttpServletRequest request){
        Map<String, Object> objectMap = this.handleParam(request);

        String hoscode = (String) objectMap.get("hoscode");
        String hosScheduleId = (String) objectMap.get("hosScheduleId");

        scheduleService.deleteSchedule(hoscode,hosScheduleId);
        return Result.ok();
    }


    /**
     * 处理参数 & 校验签名
     *
     * @param request 请求参数
     * @return Map<String, Object> objectMap
     */
    private Map<String, Object> handleParam(HttpServletRequest request) {

        // 获取请求参数集合
        Map<String, String[]> parameterMap = request.getParameterMap();

        // 转Map
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);

        // 接收到的参数logoData中"+" 被替换成了空格，为了避免签名校验失败，我们需要将加号再替换回去
        String logoData = (String) objectMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)) {
            objectMap.put("logoData", logoData.replaceAll(" ", "+"));
        }

        // 获取医院编号
        String hoscode = (String) objectMap.get("hoscode");

        // 获取签名
        String singKey = hospitalSetService.getSingKey(hoscode);

        // 检验签名
        if (!HttpRequestHelper.isSignEquals(objectMap, singKey)) {
            throw new RegisterException(ResultCodeEnum.SIGN_ERROR);
        }
        return objectMap;
    }
}
